<?php
namespace Game\Generator;

class CharacterGenerator
{
    /**
     *
     * @var Game\Factory\CharacterFactory
     */
    protected $_character_factory;

    public function __construct()
    {
        $this->_character_factory = new \Game\Factory\CharacterFactory();
    }

    /**
     *
     * @return \Game\Factory\CharacterFactory
     */
    public function getCharacterFactory()
    {
        return $this->_character_factory;
    }

    /**
     *
     * @param \Game\Factory\CharacterFactory $factory
     */
    public function setCharacterFactory(\Game\Factory\CharacterFactory $factory)
    {
        $this->_character_factory = $factory;
    }

    /**
     *
     * @param array $params
     * @return Game\Entity\Character
     */
    public function generate($type, array $params = array())
    {
        $char = $this->getCharacterFactory()->create($type, $params);

        $char->setAccuracy(3);
        $char->setAgility(4);
        $char->setBaseArmor(5);
        $char->setBaseDamage(6);
        $char->setExp(0);
        $char->setHp(50);
        $char->setLevel(1);
        $char->setLuck(3);
        $char->setMoney(12);
        $char->setName('Bot');
        $char->setPower(3);

        return $char;

    }

    public function generateBotFor(\Game\Entity\Character $character, $max_diff = 3)
    {
        $params = $character->getGeneralProperties();

        foreach ($params as $key => $value)
        {
            $params[$key] = $this->randParam($value, $max_diff);
        }

        $char = $this->getCharacterFactory()->create('bot', $params);

        return $char;
    }

    private function randParam($param, $max_diff)
    {
        return mt_rand($param - mt_rand(1, $max_diff), $param);
    }
}